package Linked;

/**
 * 单链表简单实现.
 */

public class singleLinkedList {

    public static void main(String[] args) {
        // 进行测试
        Node node1 = new Node(1, "老王");
        Node node2 = new Node(2, "老李");
        Node node3 = new Node(3, "老的");
        Node node4 = new Node(4, "老三");

        linkedList linkedList = new linkedList();
        linkedList.add(node1);
        linkedList.add(node2);
        linkedList.add(node3);
        linkedList.add(node4);

        linkedList.isEmpty();
        System.out.println(linkedList);
        linkedList.list();

    }

}


class linkedList {

    private Node head = new Node(0, "0"); // 初始化

    public void add(Node node) {
        Node temp = head;

        // 找到链表尾部插入Node
        while (true) {
            if (temp.next == null) {
                //
                break;
            }
            temp = temp.next;
        }
        temp.next = node;
    }

    /**
     * 单链表按照顺序添加节点
     *
     * @param node
     */
    public void addByOrder(Node node) {
        Node temp = head;
        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                //break
                break;
            }
            if (temp.next.num > node.num){
                break;
            } else if(temp.next.num == node.num) {
                flag = true;
                break;
            }
            // 如果以上三个条件都未满足,temp向后移动
            temp = temp.next;
        }

        if (flag){
            System.out.println("you relay insert data use");
        } else{
            node.next = temp.next;
            temp.next = node;
        }
    }

    /**
     * 修改方法
     * @param node
     */
    public void updateByNum(Node node){
        boolean empty = isEmpty();
        if (empty){
            // 不等于空时才进行更新
            Node temp = head.next;
            boolean flag = false;
            while (true){
                if (temp == null){
                    break;
                }
                if (temp.num == node.num){
                    // 找到了
                    flag = true;
                    break;
                }
                temp = temp.next;
            }
            if (flag){
                temp.data = node.data;
            } else {
                System.out.println("node not find");
            }

        } else {
            System.out.println("this linked is null....");
            return;
        }
    }

    public boolean isEmpty() {
        if (head.next == null) {
            System.out.println("linked is empty");
            return false;
        }
        System.out.println("linked is not empty");
        return true;
    }

    public void list() {
        boolean empty = this.isEmpty();
        Node temp = head.next;
        if (empty) {
            while (true) {
                if (temp == null) {
                    break;
                }
                // 输出节点信息
                System.out.println(temp);
                temp = temp.next;
            }
        }
    }

    public Node selectNode(Node node) {
        Node node1 = new Node(1, "我");
        return node1;
    }

    public void delNode(int num) {
        boolean empty = isEmpty();
        if (empty){
            Node temp = head.next;
            boolean flag = false;


            while(true){

                if (temp.next == null){
                    break;
                }

                if (temp.next.num == num){
                    // 找到的删除的节点的前一个节点
                    flag = true;
                    break;
                }
                temp = temp.next;

            }
            if (flag){
                temp.next = temp.next.next;
            } else{
                System.out.println("del node is not find");
            }


        } else {
            System.out.println("this linked is empty");
        }
    }
}

class Node {

    public int num;
    public String data;
    public Node next;

    public Node(int num, String data) {
        this.num = num;
        this.data = data;

    }

    @Override
    public String toString() {
        return "Node{" +
                "num=" + num +
                ", data=" + data +
                '}';
    }
}